package com.lezhi.socialgram.listener;

import com.lezhi.socialgram.mapper.friend.FriendMapper;
import com.lezhi.socialgram.mapper.moments.CommentMapper;
import com.lezhi.socialgram.mapper.moments.MomentMapper;
import com.lezhi.socialgram.mapper.sys.SignUpMapper;
import com.lezhi.socialgram.mapper.sys.UserHobbyMapper;
import com.lezhi.socialgram.mapper.sys.UserPrivacyMapper;
import com.lezhi.socialgram.mapper.sys.UserPushMapper;
import com.lezhi.socialgram.model.moments.Moment;
import com.lezhi.socialgram.util.Param;
import com.lezhi.socialgram.util.TableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class ApplicationListener implements org.springframework.context.ApplicationListener<ApplicationReadyEvent> {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationListener.class);

    @Autowired
    FriendMapper friendMapper;

    @Autowired
    UserPrivacyMapper userPrivacyMapper;

    @Autowired
    UserPushMapper userPushMapper;

    @Autowired
    MomentMapper momentMapper;

    @Autowired
    SignUpMapper signUpMapper;

    @Autowired
    UserHobbyMapper userHobbyMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void onApplicationEvent(final ApplicationReadyEvent event) {
        createMyLike();
        createLikeMe();
        createFriendTable();
        createUserPrivacy();
        createUserPush();
        createMoment();
        createUser();
        createTelUserId();
        createUserHobby();
        createComment();
        stringRedisTemplate.opsForValue().set(Param.SMS_VERIFY_RESEND_GAP,"30");
        stringRedisTemplate.opsForValue().set(Param.SMS_CHANNELS,"1:1/2:50/3:0/4:50");
    }

    public void createMoment() {
        String table = TableUtils.getTableNameWithDate(Moment.class, null).getTableName();
        logger.info("启动时候检查本周的表存不存在");
        try {
            momentMapper.queryMomentById(new Moment(table, 1000l));
            logger.info(table + "已经存在了，无需创建");
        } catch (Exception e) {
            //如果表不存在异常，则创建表
            if (e.getMessage().indexOf("exist") >= 0) {
                momentMapper.createMomentsTable(table);
            }
            //其他异常忽略

        }
    }

    public void createComment() {
        for (int i = 0; i < 100; i++) {
            String suffix = "";
            if (i < 10) {
                suffix = "0" + i;
            } else {
                suffix = i + "";
            }
            String table = Param.T_COMMENT_TABLE_PREFIX + suffix;
            try {
                commentMapper.createCommentTable(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    public void createMyLike() {
        for (int i = 0; i < 100; i++) {
            String suffix = "";
            if (i < 10) {
                suffix = "0" + i;
            } else {
                suffix = i + "";
            }
            String table = Param.T_MY_LIKE_TABLE_PREFIX + suffix;
            try {
                friendMapper.createMyLike(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    public void createLikeMe() {
        for (int i = 0; i < 100; i++) {
            String suffix = "";
            if (i < 10) {
                suffix = "0" + i;
            } else {
                suffix = i + "";
            }
            String table = Param.T_LIKE_ME_TABLE_PREFIX + suffix;
            try {
                friendMapper.createLikeMe(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    public void createFriendTable() {
        for (int i = 0; i < 100; i++) {
            String suffix = "";
            if (i < 10) {
                suffix = "0" + i;
            } else {
                suffix = i + "";
            }
            String table = Param.T_FRIEND_TABLE_PREFIX + suffix;
            try {
                friendMapper.createFriend(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    public void createUserPrivacy() {
        for (int i = 0; i < 10; i++) {
            String table = Param.T_USER_PRIVACY_TABLE_PREFIX + i;
            try {
                userPrivacyMapper.createUserPrivacyTable(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    public void createUserPush() {
        for (int i = 0; i < 10; i++) {
            String table = Param.T_USER_PUSH_TABLE_PREFIX + i;
            try {
                userPushMapper.createUserPushTable(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    void createUserHobby() {
        System.out.println("create userHobby");
        for (int i = 0; i < 10; i++) {
            String table = Param.T_USER_HOBBY_TABLE_PREFIX + i;
            try {
                userHobbyMapper.createUserHobbyTable(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    void createUser() {
        for (int i = 0; i < 10; i++) {
            String table = Param.T_USER_TABLE_PREFIX + i;
            try {
                signUpMapper.createUserTable(table);
            } catch (Exception e) {
                return;
            }
        }
    }

    void createTelUserId() {
        for (int i = 0; i < 10; i++) {
            String table = Param.T_USER_TEL_NUM_INDEX_TABLE_PREFIX + i;
            try {
                signUpMapper.createUserTelIndexTable(table);
            } catch (Exception e) {
                return;
            }
        }
    }

}